﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using DataContracts;
using System.Web.UI.WebControls;

namespace TrainingWorkshop.MVPWebsite.User.Views
{
	public partial class CreateNewUser : Microsoft.Practices.CompositeWeb.Web.UI.Page, ICreateNewUserView
	{
		private CreateNewUserPresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public CreateNewUserPresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

		// TODO: Forward events to the presenter and show state to the user.
		// For examples of this, see the View-Presenter (with Application Controller) QuickStart:
		//	

        protected void CreateAddButton_Click(object sender, EventArgs e)
        {
            this._presenter.OnAddNewUser();
        }
        public string FirstName
        {
            get { return this.CreateFirstNameTextBox.Text; }
        }
        public string LastName
        {
            get { return this.CreateLastNameTextBox.Text; }
        }
        public int DepartmentId
        {
            get { return Convert.ToInt32(this.CreateDepartmentDropDownList.SelectedValue); }
        }
        public string DepartmentName
        {
            get { return this.CreateDepartmentDropDownList.SelectedItem.ToString(); }
        }
        public int RoleId
        {
            get { return Convert.ToInt32(this.CreateRoleDropDownList.SelectedValue); }
        }
        public string RoleName
        {
            get { return this.CreateRoleDropDownList.SelectedItem.ToString(); }
        }
        public void SkipToAnotherPage(string address)
        {
            Response.Redirect(address);

        }
        public void ShowOperationMessage(string message)
        {
            //Response.Write("<script>alert('" + message + "');</script>");
            System.Windows.Forms.MessageBox.Show(message, "Error");
        }
        public List<DepartmentDTO> Departments
        {
            set
            {
                this.CreateDepartmentDropDownList.DataSource = value;
                CreateDepartmentDropDownList.DataTextField = "DepartmentName";
                CreateDepartmentDropDownList.DataValueField = "DepartmentId";
                CreateDepartmentDropDownList.DataBind();
                CreateDepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
            }
        }
        public List<RoleDTO> Roles
        {
            set
            {
                this.CreateRoleDropDownList.DataSource = value;
                CreateRoleDropDownList.DataTextField = "RoleName";
                CreateRoleDropDownList.DataValueField = "RoleId";
                CreateRoleDropDownList.DataBind();
                CreateRoleDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
            }
        }
}
}

